/**
 * Created by Administrator on 2017/7/22.
 */
'use static';
var app = angular.module('DBApp', ['ngAnimate', 'ui.bootstrap', 'oc.lazyLoad', 'toaster']);
app.config(['$ocLazyLoadProvider', '$httpProvider', function ($ocLazyLoadProvider, $httpProvider) {
    $ocLazyLoadProvider.config({
        debug: true
    });
    //添加请求头，防止后台无法判断是ajax请求
    $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest'
}]);
app.controller('myCtrl', ['$scope', '$modal', '$http', 'toaster', function ($scope, $modal, $http, toaster) {
    $scope.tablePx = {
        tableWidth: 800,
        tableFirstWidth: 250,
        tableTypeNameWidth: 200,
        tablePeriodWidth: 400
    };


    $scope.tableParams = {
        titlePeriod: [
            {
                period: '1'
            }
        ],
        tableData: [[{
            firstColData: {
                startAmount: '',
                endAmount: ''
            },
            typeName: '国股',
            period: [{
                rateY: '',
                feeRateM: '',
                buyAmount: '',
                remark: ''
            }]
        }, {
            typeName: '大商',
            period: [{
                rateY: '',
                feeRateM: '',
                buyAmount: '',
                remark: ''
            }]
        }, {
            typeName: '城商',
            period: [{
                rateY: '',
                feeRateM: '',
                buyAmount: '',
                remark: ''
            }]
        }, {
            typeName: '三农',
            period: [{
                rateY: '',
                feeRateM: '',
                buyAmount: '',
                remark: ''
            }]
        }, {
            typeName: '其他',
            period: [{
                rateY: '',
                feeRateM: '',
                buyAmount: '',
                remark: ''
            }]
        }, {
            typeName: '商票',
            period: [{
                rateY: '',
                feeRateM: '',
                buyAmount: '',
                remark: ''
            }]
        }]]
    };

    //备份数据
    var tempTableParamsBak = {};
    angular.copy($scope.tableParams, tempTableParamsBak);
    var tempTablePxBak = {};
    angular.copy($scope.tablePx, tempTablePxBak);

    /**
     * 内部每一行新增时
     */
    $scope.insideRowAdd = function (parentIndex, index) {
        var tempAddRow = {};
        angular.copy($scope.tableParams.tableData[parentIndex][index], tempAddRow);
        $scope.tableParams.tableData[parentIndex].splice(index + 1, 0, tempAddRow);
        clearTempAddRow(tempAddRow, ['firstColData'], ['typeName']);
    }

    /**
     * 内部行删除
     * @param parentIndex
     * @param index
     */
    $scope.insideRowDel = function (parentIndex, index) {
        var tempAddRow = {};
        angular.copy($scope.tableParams.tableData[parentIndex][index], tempAddRow);

        /*
         * 统计是否为最后一行
         */
        var num = 0;
        angular.forEach($scope.tableParams.tableData[parentIndex], function (item) {
            if (item.typeName == tempAddRow.typeName) {
                ++num;
            }
        });

        if (num > 1) {
            if($scope.tableParams.tableData[parentIndex][index].firstColData) {
                var tempFirstColData = {};
                angular.copy($scope.tableParams.tableData[parentIndex][index].firstColData, tempFirstColData);
                $scope.tableParams.tableData[parentIndex][index+1].firstColData = tempFirstColData;
            }

            $scope.tableParams.tableData[parentIndex].splice(index, 1);
        } else {
            alert('最后一行了，别删除了！！！');
        }
    }

    /**
     * 添加一列
     * @param index
     */
    $scope.insideColAdd = function (index) {
        var tempPeriod = {};
        angular.copy($scope.tableParams.tableData[0][0].period[0], tempPeriod);
        clearTempAddRow(tempPeriod);
        angular.forEach($scope.tableParams.tableData, function (item) {
            angular.forEach(item, function (a) {
                var tempObj = {};
                angular.copy(tempPeriod, tempObj);
                a.period.splice(index+1, 0, tempObj);
            });
        });

        //表头也添加一列
        var tempTitlePeriod = {};
        angular.copy($scope.tableParams.titlePeriod[index], tempTitlePeriod);
        $scope.tableParams.titlePeriod.splice(index+1, 0, tempTitlePeriod);

        $scope.tablePx.tableWidth = $scope.tablePx.tableWidth + $scope.tablePx.tablePeriodWidth + 15;
    }

    /**
     * 删除一列
     * @param index
     */
    $scope.insideColDel = function (index) {
        if($scope.tableParams.titlePeriod.length > 1) {
            $scope.tableParams.titlePeriod.splice(index, 1);
            angular.forEach($scope.tableParams.tableData, function (item) {
                angular.forEach(item, function (a) {
                    a.period.splice(index, 1);
                });
            });
            $scope.tablePx.tableWidth = $scope.tablePx.tableWidth - $scope.tablePx.tablePeriodWidth - 15;
        } else {
            alert('最后一列了！再删除就没有了！！');
        }
    }

    /**
     * 新增一大行
     * @param parentIndex
     * @param index
     */
    $scope.outRowAdd = function (parentIndex) {
        var tempRow = [];
        angular.copy($scope.tableParams.tableData[0], tempRow);
        clearTempAddRow(tempRow, null, ['typeName']);
        $scope.tableParams.tableData.splice(parentIndex+1, 0, tempRow);
    }

    /**
     * 删除一大行
     * @param parentIndex
     * @param index
     */
    $scope.outRowDel = function (parentIndex) {
        if($scope.tableParams.tableData.length > 1) {
            $scope.tableParams.tableData.splice(parentIndex, 1);
        } else {
            alert('最后一大行了，不能再删除了！')
        }
    }

    /**
     * 删除报价
     */
    $scope.deleteData = function () {
        clearTempAddRow($scope.tableParams.tableData, null, ['typeName'])
    }

    /**
     * 重置表单
     */
    $scope.resetTable = function () {
        var tableReset = {};
        angular.copy(tempTableParamsBak, tableReset);
        var tablePxReset = {};
        angular.copy(tempTablePxBak, tablePxReset);
        $scope.tableParams = tableReset;
        $scope.tablePx = tablePxReset;
    }
    
    $scope.submitTableData = function () {
        alert(JSON.stringify($scope.tableParams));
    }

    /**
     * 清空将要添加的内部行
     * @param [Object] temp 需要清空的对象
     * @param [Array] deleteKey 删除的属性
     * @param [Array] notClearKey 需要保留的字段，不予清空
     */
    function clearTempAddRow(temp, deleteKey, notClearKey) {
        if (!temp) {
            return temp;
        }
        if (deleteKey) {
            angular.forEach(deleteKey, function (item) {
                if (!(temp instanceof Array) && (temp instanceof Object)) {
                    delete temp[item];
                }
            });
        }
        angular.forEach(temp, function (value, key) {
            if (notClearKey) {
                angular.forEach(notClearKey, function (item) {
                    if (key != item) {
                        if (value instanceof Object) {
                            clearTempAddRow(value, deleteKey, notClearKey);
                        } else if (typeof value == 'boolean') {
                            temp[key] = false;
                        } else {
                            temp[key] = '';
                        }
                    }
                });
            } else {
                if (value instanceof Object) {
                    clearTempAddRow(value, deleteKey, notClearKey);
                } else if (typeof value == 'boolean') {
                    temp[key] = false;
                } else {
                    temp[key] = '';
                }
            }
        });
    }
}]);